package com.haotunet.app.goldtreasure.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.MyRegularAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.RegularProfitModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout.OnRefreshListener;
import com.haotunet.app.pulltorefresh.pullableView.PullableListView;

public class RegularProductFragment extends BaseFragment {
    private View view;
    private boolean isPrepared;
    private boolean mHasLoadedOnce;
    private LinearLayout noRecordLayout;
    private PullToRefreshLayout mfreshLayout;
    private PullableListView mLv;
    private int pageNum = 20;
    private final int GET_REGULAR_PRODUCT_SUCCESS = 1;
    private final int LOAD_MORE_REGULAR_PRODUCT_SUCCESS = 2;
    private MyRegularAdapter myRegularAdapter;
    private Handler handle = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case GET_REGULAR_PRODUCT_SUCCESS:
                    RegularProfitModel model = (RegularProfitModel) msg.obj;
                    if (model.getRegularInvestmentList().size() == 0) {
                        noRecordLayout.setVisibility(View.VISIBLE);
                        return;
                    }
                    myRegularAdapter = new MyRegularAdapter(getActivity(), model.getRegularInvestmentList());
                    mLv.setAdapter(myRegularAdapter);
                    break;
                case LOAD_MORE_REGULAR_PRODUCT_SUCCESS:
                    RegularProfitModel moreModel = (RegularProfitModel) msg.obj;
                    if (moreModel.getRegularInvestmentList().size() == 0) {
                        UIHelper.showToast(getActivity(), getString(R.string.last_page_hint));
                    }
                    myRegularAdapter.addDatas(moreModel.getRegularInvestmentList());
                    break;
                default:
                    break;
            }
        }

    };
    private int type;

    public static RegularProductFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        RegularProductFragment fragment = new RegularProductFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_regular_undue, null);
        initView();
        isPrepared = true;
        type = getArguments().getInt("type");
        if (type == 1) {
            lazyLoad();
        }
        return view;
    }

    private void initView() {
        mfreshLayout = (PullToRefreshLayout) view.findViewById(R.id.id_swipe_ly);
        noRecordLayout = (LinearLayout) view.findViewById(R.id.no_record_layout);
        mLv = (PullableListView) view.findViewById(R.id.lv_my_regular);
        RefreshData(0);
    }

    private void RefreshData(final int pagenum) {
        mfreshLayout.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                getMyRegularProduct(pagenum + "", "refresh");
                pageNum = 20;
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                getMyRegularProduct(pageNum + "", "loadmore");
            }
        });
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        getMyRegularProduct("0", "");
    }

    private void getMyRegularProduct(String pagenum, final String action) {
        ActionUtil actionUtil = new ActionUtil(getActivity());
        actionUtil.getRegularProfitRecord(type, pagenum);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (!isActivityAvailable()) {
                    return;
                }
                RegularProfitModel profitModel = (RegularProfitModel) model;
                if (profitModel == null) {
                    UIHelper.showToast(getActivity(), getString(R.string.rank_fail));
                    return;
                }
                if (profitModel.getErrorMsg() != null) {
                    if (profitModel.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(getActivity(), getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(getActivity(), profitModel.getErrorMsg());
                    }
                } else {
                    mHasLoadedOnce = true;
                    if (action.equals("refresh") || action.equals("loadmore")) {
                        mfreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                    Message msg = new Message();
                    if (action.equals("loadmore")) {
                        msg.what = LOAD_MORE_REGULAR_PRODUCT_SUCCESS;
                        pageNum = pageNum + 20;
                    } else {
                        msg.what = GET_REGULAR_PRODUCT_SUCCESS;
                    }
                    msg.obj = profitModel;
                    handle.sendMessage(msg);
                }
            }
        });
    }

}
